using System.Web.Mvc;
using Netkk.Common.Interfaces.Services;
using Netkk.Core;
using Netkk.Core.Auth;
using Netkk.Common;
using Netkk.Common.Models;
using System;
using System.Collections.Generic;
using Netkk.Models;


namespace Netkk.Controllers
{
    [HandleError]
    public class LinkController : BaseControler
    {
        private readonly ILinkService linkService;

        public LinkController(ILinkService linkService)
        {
            this.linkService = linkService;
        }

        [IsAuthenticated]
        public ActionResult Index(int? id)
        {
            IEnumerable<Link> links = linkService.GetAllLinks();

            LinkListModel model = LoadPagedLinkList(id ?? 1, links);

            return View(model);
        }

        private LinkListModel LoadPagedLinkList(int id, IEnumerable<Link> links)
        {
            int totalPages;
            int currentPage = id == 0 ? 1 : id;

            links = links.PagedItems(10, currentPage - 1, out totalPages);

            List<LinkModel> downloadModels = new List<LinkModel>();

            bool allowEditOrAdd = this.IsInRole(Roles.Admin);

            links.ForEach(d => downloadModels.Add(LinkModel.Create(d, allowEditOrAdd)));

            LinkListModel model = new LinkListModel
            {
                Links = downloadModels,
                TotalPages = totalPages,
                CurrentPage = currentPage,
                ShowCreateLink = allowEditOrAdd
            };

            return model;
        }

        [IsAuthenticated]
        public ActionResult ShowCategory(string id)
        {
            IEnumerable<Link> links = linkService.GetAllLinksFromCategory(id);

            IList<LinkModel> linkModels = new List<LinkModel>();
            links.ForEach(l => linkModels.Add(LinkModel.Create(l, this.IsInRole(Roles.Admin))));

            LinkListModel model = new LinkListModel
                                      {
                                          Links = linkModels,
                                          Category = id
                                      };

            return View(model);
        }

        public ActionResult Details(Guid id)
        {
            Link link = linkService.GetLinkById(id); 
            link = link ?? new Link
            {
                Category = "No Link",
                Description = "Link not Found",
                Name = "Link not Found",
                Url = "http://www.dotnetkk.de/"
            };
            LinkModel model = LinkModel.Create(link);
            return View(model);
        }

        [UserInRole(Roles.Admin)]
        public ActionResult Delete(Guid id)
        {
            Link link = linkService.GetLinkById(id);
            linkService.DeleteLink(link);
            return this.RedirectToActionWithId("Link", "ShowCategory", link.Category);
        }

        [UserInRole(Roles.Admin)]
        public ActionResult Create(string id)
        {
            LinkModel model = LinkModel.Create();
            model.Category = id;
            return View(model);
        }

        [UserInRole(Roles.Admin)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(string id, FormCollection collection)
        {
            LinkModel model = LinkModel.Create();
            model.Category = id;
            try
            {
                UpdateModel(model, new[] { "Name", "Url", "Description" });

                Link link = model.MapToLink();
                if (linkService.SaveLink(link))
                {
                    return this.RedirectToActionWithId("Link", "ShowCategory", id);
                }

            }
            catch (Exception)
            {
                return View(model);
            }
            return View(model);
        }

        [UserInRole(Roles.Admin)]
        public ActionResult Edit(Guid id)
        {
            Link link = linkService.GetLinkById(id);
            link = link ?? new Link
                               {
                                   Category = "No Link",
                                   Description = "Link not Found",
                                   Name = "Link not Found",
                                   Url = "http://www.dotnetkk.de/"
                               };

            LinkModel model = LinkModel.Create(link);
            model.AllowEditOrAdd = this.IsInRole(Roles.Admin);
            
            return View(model);
        }

        [UserInRole(Roles.Admin)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            Link link = linkService.GetLinkById(id);
            LinkModel model = LinkModel.Create(link);
            model.AllowEditOrAdd = this.IsInRole(Roles.Admin);
            try
            {
                UpdateModel(model, new[] { "Name", "Url", "Description", "Category" });

                link = model.MapToLink();

                if (!linkService.SaveLink(link))
                {
                    return this.RedirectToActionWithId("Link", "ShowCategory", link.Category);
                }
            }
            catch
            {
                return View(model);
            }
            return View(model);
        }
    }
}
